// component/planing/index.js
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    role: {
      type: String,
      value: "",
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    searchValue: "",
    list: [],
    isLastPage: false,
    current: 1,
    size: 10,
    hadNumber: 0,
    timeNumber: 0,
    totalNumber: 0
  },

  lifetimes: {
    attached: function () {
      this.getData();
      this.getTotal();
    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
    },
  },
  methods: {
		onSearch(e) {
			this.setData({
				current: 1,
				list: [],
				isLastPage: false,
				searchValue: e.detail
			})

			console.log('搜索', this.data.searchValue)
			this.getData();
		},
		  //下拉刷新
			onPullDownRefresh() {
				console.log(2222)
				this.setData({
					current: 1,
					list: [],
					isLastPage: false
				})
				this.getData();
				this.getTotal();
			},
			//上滑加载
			onReachBottom() {
				let {
					current,
					isLastPage
				} = this.data
				if (isLastPage) {
					return
				}
				this.setData({
					current: (Number(current) + 1)
				})
				this.getData()
			},
    getTotal() {
      let id = app.globalData.userInfo.companyData.companyId;
      wx.$apis.enterStockReport(id).then(res => {
        if(res.data){
          let data=res.data;
          this.setData({
            hadNumber: data.netWeight,
            timeNumber: data.transactionNum,
            totalNumber: data.goodsPrice,
          })
        }
      }).catch(err => {
        wx.hideLoading()
      })

    },
    getData() {
      const {
        role,
        current,
        size
      } = this.data;
      let parmas;
      if (role == 1) {//市场
        parmas = {
          hasComplete: true,
          companyId: app.globalData.userInfo.companyData.companyId,
          current,
          size,
					goodsOwnerName:this.data.searchValue
        }
      } else {//小工
        parmas = {
          companyId: app.globalData.userInfo.companyData.companyId,
          outStockUserId: app.globalData.userInfo.userDetail.id,
          current,
          size,
					goodsOwnerName:this.data.searchValue
        }
      }
      wx.showLoading({
        title: "正在请求数据中"
      })
      wx.$apis.marketerPage(parmas).then(res => {
        wx.stopPullDownRefresh();
        let old = this.data.list;
				let allList = res.data && res.data.records.map(item => {
					item.transactionPrice= wx.$getYuan(item.transactionPrice)
					return item
				})
        if (allList && allList.length) {
          let newList = old.concat(allList)
          this.setData({
            list: newList
          })
          if (allList.length < size) {
            this.setData({
              isLastPage: true
            })
          }
        } else {
          this.setData({
            isLastPage: true
          })
        }

      }).catch(err => {
        wx.hideLoading()
      })
    },
    //跳转发货详情页面
    getmore() {
      wx.navigateTo({
        url: "/pages/order/delivery/delivery"

      });
    },

  },

})
